using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Text;
using System.Windows.Forms;

namespace Meta.Editor
{
    class IDEApplication : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        // Constructor
        IDEApplication()
        {
            IsSingleInstance = true;
            EnableVisualStyles = true;
            OnCreateMainForm();    // Force Listener to be created first
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        [STAThreadAttribute]
        static void Main(string[] args)
        {
            new IDEApplication().Run(args);
        }

        protected override void OnCreateMainForm()
        {
            if (MainForm != null)
                return;
            Listener listener = new Listener();
            MainForm = Listener.mainListener = listener;
            // The Meta.TopLevel class is now loaded and initialized.

            //This is ThreadStatic, so setting it only blocks package loading from occurring in the GUI thread.
            Meta.Package.inhibitLoading = true;

#if BUILDIN2D
			// This really shouldn't be wired into the IDE, but I haven't figured out how to get Dev Studio to
			// compile it into the Packages directory.
			Namespace basic2D = Namespace.MakeStandardNamespace(Symbol.Intern("Basic2D"));
			Fluid.Bind(new object[] { Namespace.currentNamespaceFluid, basic2D }, new Meta.Thunk(LoadBasic2D));
			Namespace.RootNamespace.Define("Basic2D", basic2D, "Basic 2D graphics package");
			Namespace.UserNamespace.Import(basic2D);
#endif

            //Namespace.DefineManifestConstant("Editor", typeof(Editor), "The Meta code editor");
            //Namespace.DefineManifestConstant("Listener", typeof(Listener), "The Listener window class");
            Assembly me = Assembly.GetAssembly(typeof(Meta.TopLevel));
            AssemblyCopyrightAttribute copyright = (AssemblyCopyrightAttribute)(me.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]);
            Version version = me.GetName().Version;

            listener.Out.WriteLine("Meta development environment, version {0}.{1}   (revision {2} build {3})", new object[] { version.Major, version.Minor, version.Revision, version.Build });
            listener.Out.WriteLine("Copyright \u00a9 {0}.\n", copyright.Copyright);

            // Force the jitter to compile the editor now so we don't have any glitches while the user types their first expression.
            listener.ReindentAllMenuItem_Click(null, null);
            listener.SetArglistText("Welcome to Meta");

            listener.Out.Write(Meta.TopLevel.promptString);
            listener.Out.Flush();
            Thread.CurrentThread.Name = "GUI Thread";
            Meta.TopLevel.idle += new Meta.VoidThunk(Listener.ResetCursors);
            Meta.TopLevel.evalStart += new VoidThunk(((IGUIInterface)listener).ResetLog);
            listener.ShowNamespace();

            //ProcessCommandLineArgs();

            System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Meta.TopLevel.UnhandledExceptionHandler);
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            base.OnStartup(eventArgs);
            Listener.ProcessCommandLine(eventArgs.CommandLine);
            return true;
        }

        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
        {
            eventArgs.BringToForeground = false;
            Listener.ProcessCommandLine(eventArgs.CommandLine);
            base.OnStartupNextInstance(eventArgs);
        }
    }
}
